//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.imagemanager.util;

import android.util.Log;
import java.lang.reflect.Array;

public final class LongSparseArray<E> {
    private static final Object DELETED = new Object();
    private boolean mGarbage;
    private long[] mKeys;
    private Object[] mValues;
    private int mSize;

    public LongSparseArray() {
        this(10);
    }

    public LongSparseArray(int initialCapacity) {
        this.mGarbage = false;
        initialCapacity = LongSparseArray.ArrayUtils.idealIntArraySize(initialCapacity);
        this.mKeys = new long[initialCapacity];
        this.mValues = new Object[initialCapacity];
        this.mSize = 0;
    }

    public E get(long key) {
        return this.get(key, null);
    }

    public E get(long key, E valueIfKeyNotFound) {
        int i = binarySearch(this.mKeys, 0, this.mSize, key);
        return i >= 0 && this.mValues[i] != DELETED? (E) this.mValues[i] :valueIfKeyNotFound;
    }

    public void delete(long key) {
        int i = binarySearch(this.mKeys, 0, this.mSize, key);
        if(i >= 0 && this.mValues[i] != DELETED) {
            this.mValues[i] = DELETED;
            this.mGarbage = true;
        }

    }

    public void remove(long key) {
        this.delete(key);
    }

    private void gc() {
        int n = this.mSize;
        int o = 0;
        long[] keys = this.mKeys;
        Object[] values = this.mValues;

        for(int i = 0; i < n; ++i) {
            Object val = values[i];
            if(val != DELETED) {
                if(i != o) {
                    keys[o] = keys[i];
                    values[o] = val;
                }

                ++o;
            }
        }

        this.mGarbage = false;
        this.mSize = o;
    }

    public void put(long key, E value) {
        int i = binarySearch(this.mKeys, 0, this.mSize, key);
        if(i >= 0) {
            this.mValues[i] = value;
        } else {
            i = ~i;
            if(i < this.mSize && this.mValues[i] == DELETED) {
                this.mKeys[i] = key;
                this.mValues[i] = value;
                return;
            }

            if(this.mGarbage && this.mSize >= this.mKeys.length) {
                this.gc();
                i = ~binarySearch(this.mKeys, 0, this.mSize, key);
            }

            if(this.mSize >= this.mKeys.length) {
                int n = LongSparseArray.ArrayUtils.idealIntArraySize(this.mSize + 1);
                long[] nkeys = new long[n];
                Object[] nvalues = new Object[n];
                System.arraycopy(this.mKeys, 0, nkeys, 0, this.mKeys.length);
                System.arraycopy(this.mValues, 0, nvalues, 0, this.mValues.length);
                this.mKeys = nkeys;
                this.mValues = nvalues;
            }

            if(this.mSize - i != 0) {
                System.arraycopy(this.mKeys, i, this.mKeys, i + 1, this.mSize - i);
                System.arraycopy(this.mValues, i, this.mValues, i + 1, this.mSize - i);
            }

            this.mKeys[i] = key;
            this.mValues[i] = value;
            ++this.mSize;
        }

    }

    public int size() {
        if(this.mGarbage) {
            this.gc();
        }

        return this.mSize;
    }

    public long keyAt(int index) {
        if(this.mGarbage) {
            this.gc();
        }

        return this.mKeys[index];
    }

    public E valueAt(int index) {
        if(this.mGarbage) {
            this.gc();
        }

        return (E) this.mValues[index];
    }

    public void setValueAt(int index, E value) {
        if(this.mGarbage) {
            this.gc();
        }

        this.mValues[index] = value;
    }

    public int indexOfKey(long key) {
        if(this.mGarbage) {
            this.gc();
        }

        return binarySearch(this.mKeys, 0, this.mSize, key);
    }

    public int indexOfValue(E value) {
        if(this.mGarbage) {
            this.gc();
        }

        for(int i = 0; i < this.mSize; ++i) {
            if(this.mValues[i] == value) {
                return i;
            }
        }

        return -1;
    }

    public void clear() {
        int n = this.mSize;
        Object[] values = this.mValues;

        for(int i = 0; i < n; ++i) {
            values[i] = null;
        }

        this.mSize = 0;
        this.mGarbage = false;
    }

    public void append(long key, E value) {
        if(this.mSize != 0 && key <= this.mKeys[this.mSize - 1]) {
            this.put(key, value);
        } else {
            if(this.mGarbage && this.mSize >= this.mKeys.length) {
                this.gc();
            }

            int pos = this.mSize;
            if(pos >= this.mKeys.length) {
                int n = LongSparseArray.ArrayUtils.idealIntArraySize(pos + 1);
                long[] nkeys = new long[n];
                Object[] nvalues = new Object[n];
                System.arraycopy(this.mKeys, 0, nkeys, 0, this.mKeys.length);
                System.arraycopy(this.mValues, 0, nvalues, 0, this.mValues.length);
                this.mKeys = nkeys;
                this.mValues = nvalues;
            }

            this.mKeys[pos] = key;
            this.mValues[pos] = value;
            this.mSize = pos + 1;
        }
    }

    private static int binarySearch(long[] a, int start, int len, long key) {
        int high = start + len;
        int low = start - 1;

        while(high - low > 1) {
            int guess = (high + low) / 2;
            if(a[guess] < key) {
                low = guess;
            } else {
                high = guess;
            }
        }

        if(high == start + len) {
            return ~(start + len);
        } else if(a[high] == key) {
            return high;
        } else {
            return ~high;
        }
    }

    private void checkIntegrity() {
        for(int i = 1; i < this.mSize; ++i) {
            if(this.mKeys[i] <= this.mKeys[i - 1]) {
                for(int j = 0; j < this.mSize; ++j) {
                    Log.e("FAIL", j + ": " + this.mKeys[j] + " -> " + this.mValues[j]);
                }

                throw new RuntimeException();
            }
        }

    }

    public static final class ArrayUtils {
        private static Object[] EMPTY = new Object[0];
        private static final int CACHE_SIZE = 73;
        private static Object[] sCache = new Object[73];

        private ArrayUtils() {
        }

        public static int idealByteArraySize(int need) {
            for(int i = 4; i < 32; ++i) {
                if(need <= (1 << i) - 12) {
                    return (1 << i) - 12;
                }
            }

            return need;
        }

        public static int idealBooleanArraySize(int need) {
            return idealByteArraySize(need);
        }

        public static int idealShortArraySize(int need) {
            return idealByteArraySize(need * 2) / 2;
        }

        public static int idealCharArraySize(int need) {
            return idealByteArraySize(need * 2) / 2;
        }

        public static int idealIntArraySize(int need) {
            return idealByteArraySize(need * 4) / 4;
        }

        public static int idealFloatArraySize(int need) {
            return idealByteArraySize(need * 4) / 4;
        }

        public static int idealObjectArraySize(int need) {
            return idealByteArraySize(need * 4) / 4;
        }

        public static int idealLongArraySize(int need) {
            return idealByteArraySize(need * 8) / 8;
        }

        public static boolean equals(byte[] array1, byte[] array2, int length) {
            if(array1 == array2) {
                return true;
            } else if(array1 != null && array2 != null && array1.length >= length && array2.length >= length) {
                for(int i = 0; i < length; ++i) {
                    if(array1[i] != array2[i]) {
                        return false;
                    }
                }

                return true;
            } else {
                return false;
            }
        }

        public static <T> T[] emptyArray(Class<T> kind) {
            if(kind == Object.class) {
                return (T[]) EMPTY;
            } else {
                int bucket = (System.identityHashCode(kind) / 8 & 2147483647) % 73;
                Object cache = sCache[bucket];
                if(cache == null || cache.getClass().getComponentType() != kind) {
                    cache = Array.newInstance(kind, 0);
                    sCache[bucket] = cache;
                }

                return (T[])cache;
            }
        }

        public static <T> boolean contains(T[] array, T value) {
            T[] var5 = array;
            int var4 = array.length;

            for(int var3 = 0; var3 < var4; ++var3) {
                T element = var5[var3];
                if(element == null) {
                    if(value == null) {
                        return true;
                    }
                } else if(value != null && element.equals(value)) {
                    return true;
                }
            }

            return false;
        }
    }
}
